package Com.Isman.Booking;


import java.util.List;

import org.kobjects.base64.Base64;

import Com.Isman.Adapter.HotelImageAdapter;
import Com.Isman.WebService.NetworkCallDetailListener;
import Com.Isman.WebService.NetworkCallImageListListener;
import Com.Isman.WebService.ServiceConnect;
import Com.Isman.WebService.ComplexType.Hotel;
import Com.Isman.WebService.ComplexType.HotelDetail;
import Com.Isman.WebService.ComplexType.Image;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

public class PageHotelDetail extends Activity implements NetworkCallDetailListener,NetworkCallImageListListener {
	class NetworkCallDetailTask extends AsyncTask<String, Void, HotelDetail>{
		private NetworkCallDetailListener listener = null;
		
		public NetworkCallDetailTask(NetworkCallDetailListener listener) {
			this.listener = listener;
		}
		@Override
		protected HotelDetail doInBackground(String... params) {
		   Log.v("Isman", "doInBackground(Integer... params)");			    
		   ServiceConnect sc = new ServiceConnect(); 
		   return sc.callGetHotelDetail(Integer.parseInt(params[0]), params[1], params[2]);
		}		
		@Override
		protected void onPostExecute(HotelDetail detail) {
			super.onPostExecute(detail);
		   	listener.onNetworkCallDetailComplete(detail);
		   	pbLoadDetail.setVisibility(View.INVISIBLE);
		   	llDetailBoby.setVisibility(View.VISIBLE);
		}  
		@Override
		protected void onPreExecute() {
		    super.onPreExecute();
		    pbLoadDetail.setVisibility(View.VISIBLE);
		    llDetailBoby.setVisibility(View.INVISIBLE);
		}
	}

	class NetworkCallImageTask extends AsyncTask<Integer, Void, List<Image>>{
		private NetworkCallImageListListener listener = null;
		
		public NetworkCallImageTask(NetworkCallImageListListener listener) {
			this.listener = listener;
		}
		
		@Override
		protected List<Image> doInBackground(Integer... params) {
			ServiceConnect sc = new ServiceConnect();
			return sc.callGetGalleryThump(params[0]);
		}
		
		@Override
		protected void onPostExecute(List<Image> list){
			listener.onNetworkCallImageListComplete(list);
			pbLoadImage.setVisibility(View.INVISIBLE);
		}
		
		@Override
		protected void onPreExecute(){
			super.onPreExecute();
			pbLoadImage.setVisibility(View.VISIBLE);
		}
	}
	public static final String HOTEL_DETAIL 		= "HotelDetailActivity";
	private Button btnBooking;
	private ImageButton btnMap;
	private Gallery glImage;
	private Hotel hotel;
	private ImageView ivLogo;
	private LinearLayout llDetailBoby;
	private ProgressBar pbLoadDetail;
	private ProgressBar pbLoadImage;
	private RatingBar rbRate;
	
	private TextView tvCity;
	private TextView tvDetail;
	private TextView tvFeature;
	private TextView tvHotelName;
	
	private TextView tvLocation;
	
	private TextView tvPolicy;
    private TextView tvProperty;
    @Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pagehoteldetail);
	
		Intent intent = getIntent();
		this.hotel = (Hotel) intent.getExtras().getSerializable(HOTEL_DETAIL);
		
		tvHotelName	= (TextView)findViewById(R.id.lbl_HeadHotelName);
		tvCity		= (TextView)findViewById(R.id.lbl_HeadCity);
		tvDetail	= (TextView)findViewById(R.id.lblDetailDetail);
		tvFeature	= (TextView)findViewById(R.id.lblDetailFeature);
		tvLocation	= (TextView)findViewById(R.id.lblDeatilLocation);
		tvPolicy	= (TextView)findViewById(R.id.lblDetailPolicy);
		tvProperty	= (TextView)findViewById(R.id.lblDetailProperty);
		btnBooking	= (Button)findViewById(R.id.btn_HeadBooking);
		btnMap		= (ImageButton)findViewById(R.id.btnDetailMap);
		pbLoadDetail= (ProgressBar)findViewById(R.id.prbLoadDetail);
		llDetailBoby= (LinearLayout)findViewById(R.id.llDetailBody);
		rbRate		= (RatingBar)findViewById(R.id.rtb_HeadRating);
		pbLoadImage = (ProgressBar)findViewById(R.id.pgbLoadImage);
		ivLogo		= (ImageView)findViewById(R.id.img_HeadLogo);
		glImage		= (Gallery)findViewById(R.id.glrDetailGallery);
		
		
		
		tvHotelName.setText(hotel.getHotelName());
		tvCity.setText(hotel.getMap().getCity());
		rbRate.setRating(hotel.getHotelRating());
		
		
        byte[] decodedString = Base64.decode(hotel.getLogo().getContent());
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ivLogo.setImageBitmap(decodedByte);
        
        btnMap.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),PageMap.class);
				v.getContext().startActivity(intent);
			}
		});
        btnBooking.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v){
				Intent intent = new Intent(v.getContext(),PageRoomList.class);
				intent.putExtra(PageRoomList.ROOMLIST_HOTEL, hotel);
				v.getContext().startActivity(intent);
			}
		});
        ivLogo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(0);
			}
		});
        
        new NetworkCallDetailTask(this).execute(new String[]{hotel.getHotelID()+"","2010-10-10","2012-10-10"});
	}
    @Override
    public Dialog onCreateDialog(int id) {
        switch (id) {
            case 0:
                ListView lv = new ListView(this);
                
              
                
                AlertDialog.Builder dialogcalendarlBuilder = new AlertDialog.Builder(this);
                dialogcalendarlBuilder.setTitle("Edit Category");
                dialogcalendarlBuilder.setView(lv);
                AlertDialog dialog = dialogcalendarlBuilder.create();
       
                
                dialog.setButton("Set", new DialogInterface.OnClickListener() {		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						//edtInput.setText("ads");
					}
				});
                dialog.setButton2("Close",new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
                
                return dialog;
        }
		return null;
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menumain, menu);
	    return true;
    }
	@Override
	public void onNetworkCallDetailCancel() {
		
	}
	@Override
	public void onNetworkCallDetailComplete(HotelDetail detail) {
		tvDetail.setText(detail.getDetail());
		tvPolicy.setText(detail.getPolicy());
		tvFeature.setText(detail.getFeatures());
		tvLocation.setText(detail.getLocation());
		tvProperty.setText(detail.getPropertyAmenities());
		
		new NetworkCallImageTask(this).execute(new Integer[]{hotel.getHotelID()});
	}
    
    
	
	@Override
	public void onNetworkCallImageListCancel() {
		// TODO Auto-generated method stub	
	}
	
	@Override
	public void onNetworkCallImageListComplete(List<Image> image) {
		glImage.setAdapter(new HotelImageAdapter(this,image.toArray(new Image[0])));
	}
}
